- Special Edition Using Visual Basic Script -

CHAPTER 23 - Microsoft SQL Server 6.5 Web Extensions

by Ibrahim Malluf


Microsoft has embraced the Internet with enthusiasm, as witnessed by the explosion of new products directly targeted at the Internet and by the revamping of existing products to accommodate the Internet. SQL Server is no exception to this drive by Microsoft to place its product line in the forefront of Internet development. Besides the MS Internet Information Server's IDC, which allows for interactive data operations, Microsoft has added a set of extensions to SQL-Server itself that provide a means for easily publishing your data on the Internet. They are called the SQL Server 6.5 Web Extensions.

In this chapter, you learn:

Introducing the SQL Server 6.5 Web Extensions

The core of this Web publishing capability is the addition of three new stored procedures; sp_dropwebtask, sp_makewebtask, and sp_runwebtask. By using these three new stored procedures you can generate HTML Web pages. You can generate these pages on demand, at regular timed intervals, or from triggers designed to react to the changes of critical data. There is even a Web Assistant tool to help you create these pages, which I will discuss later in the chapter. For now let's go over each of these new procedures.

Creating Web Tasks by Using sp_makewebtask

The sp_makewebtask creates a task in SQL Server that makes HTML pages from a set of properties described in the procedures syntax. Listing 23.1 shows this syntax. I am expecting that most of you reading this chapter have a fair knowledge of SQL Server's stored procedure syntax; so I'll just go over the parts that are really HTML page specific. More detailed information is available using the SQL Server's Books Online or the What's New In SQL Server 6.5 manual. The parameter specifications I am showing here come from there. I added additional comments and modifications when I found things to be a little more flexible than described in the original documentation.

Listing 23.1 The sp_makewebtask Syntax

sp_makewebtask {@outputfile = 'outputfile', @query = 'query'}
[, [@fixedfont = fixedfont,] [@bold = bold,] [@italic = italic,]
[@colheaders = colheaders,] [@lastupdated = lastupdated,]
[@HTMLHeader = HTMLHeader,] [@username = username,]
[@dbname = dbname,] [@templatefile = 'templatefile',]
[@webpagetitle = 'webpagetitle',] [@resultstitle = 'resultstitle',]
[[@URL = 'URL', @reftext = 'reftext'] |
[@table_urls = table_urls, @url_query = 'url_query',]]
[@whentype = whentype,] [@targetdate = targetdate,]
[@targettime = targettime,]
[@dayflags = dayflags,] [@numunits = numunits,] [@unittype = unittype,]
[@procname = procname, ] [@maketask = maketask,] [@rowcnt = rowcnt,]
[@tabborder = tabborder,] [@singlerow = singlerow,] [@blobfmt = blobfmt]]

The following list explains the parameters in more detail:

Value HTML Formatting Code
1 H1
2 H2
3 H3
4 H4
5 H5
6 H6

Table 23.1 The Values and Their Meanings of the @whentypen Parameter

Value Description
1 Create page now. The web task will be created, executed immediately, and deleted immediately after execution. This is the default.
2 Create page later. The stored procedure for creating the HTML document will be created immediately, but execution of the web task is deferred until the date and time specified by @targetdate and @targettime (optional). If no @targettime is specified, the web task will be executed at 12:00 a.m. @targetdate is required when @whentype = 2. This web task will be deleted automatically after the targeted date and time has passed.
3 Create page every n day(s) of the week. The HTML document will be created on day(s) specified in @dayflags and at the time specified by @targettime (optional), beginning with the date in @targetdate. If no @targettime value is specified, the default is 12:00 A.M. The @targetdate parameter is required when @whentype = 3. The day(s) of the week are specified in the @dayflags parameter. More than one day of the week can be specified with the @dayflags parameter. Web tasks created with @whentype = 3 will not be deleted automatically and continue to run on the specified day(s) of the week until the user deletes them by using sp_dropwebtask.
4 Create page every n minutes, hours, days, or weeks. The HTML document is created every n time period beginning with the date and time specified in @targetdate and @targettime (optional). If no @targettime is specified, the Web task will be executed at 12:00 A.M. The @targetdate parameter is required in this case. The task will run automatically every n minutes, hours, days, or weeks as specified by the @numunits and @unittype parameters. The tasks will run until the user deletes them by using sp_dropwebtask.
5 Create page upon request. The procedure is created without automatic scheduling. The user creates a HTML document by running sp_runwebtask and deletes it only by using sp_dropwebtask.
6 Create page now and later. The HTML document is created immediately and re-created according to @whentype = 2.
7 Create page now and every n day(s) of the week. The HTML document is created immediately and re-created according to @whentype = 3, except no @targetdate is required.
8 Create page now and periodically thereafter. The HTML document is created immediately and re-created according to @whentype = 4, except no @targetdate is required.
9 Create page now and upon request. The HTML document is created immediately and re-created according to @whentype = 5. The task must be deleted manually.

While not a comprehensive description of the sp_makewebtask, it is enough to get us started with the examples that will be provided later. I would like to make note of the fact that a whentype() other than 1 will result in a task being stored as a repeatable task within your database that will be called according to the specified @whentyen. To remove this task you will need to use the sp_dropwebtask stored procedure or directly delete it from the tasks list.

Using the sp_dropwebtask to Remove Webtasks

The sp_dropwebtask is a much simpler stored procedure than the preceding one. Its purpose is simpler as well. It merely deletes a previously defined Web task that was created by the sp_makewebtask stored procedure. The syntax is shown in Listing 23.2.

sp_dropwebtask {@procname = procname | @outputfile = outputfile

[ccc]| @procname = procname, @outputfile = outputfile}

Parameter Definition
@outputfile Specifies the name of the web task to be deleted. The outputfile variable is of varchar datatype and has a maximum of 255 characters.
@procname Specifies the name of the web task procedure to delete. The named procedure describes the query for the web task. The procname variable is of varchar datatype and has a maximum of 28 characters.

Using sp_runwebtask to Specifically Run a Previously Created Web Task

sp_runwebtask {@procname = procname | @outputfile = outputfile |

Parameter Definition
@procname = procname, Specifies the name of the web
@outputfile = outputfile} task to run. The outputfile variable
where @outputfile is of varchar datatype and has a
maximum of 255 characters.
@procname Specifies the name of the web task procedure to run. The named procedure defines the query for the web task. The procname variable is of varchar datatype and has a maximum of 28 characters.

Using the SQL Server Web Assistantùfor the Faint of Heart

Does the prospect of directly using the sp_makewebtask stored procedure intimidate you? Relax! The Web Assistant is a wizard-type program that provides a step-by-step interface for creating standard HTML pages from your database. The Web Assistant uses Transact-SQL queries, stored procedures, and extended stored procedures as the foundation for the generated HTML pages. You can let the Web Assistant design an HTML page that includes a table containing all the data in standard table rows for you. The alternative is to provide a template file that contains a custom format for the creation of your HTML pages. Figure 23.1 shows the opening screen of the Web Assistant. In order to use the Web Assistant, you need to have sufficient privileges to create procedures, files, and select privileges on columns. To run Web Assistant double-click the Web Assistant icon in the SQL Server program group.

FIG. 23.1

The opening screen for the Web Assistant.

When starting the Web Assistant, specify the server that contains the database and provide either a log on id, and password, or alternatively select the log on security of NT itself. In Figure 23.1 you can see that I chose the latter. Like all other Wizards, you select the next button to proceed on to the next step. The Web assistant logs on to the server and presents you with a window that gives you a choice of three different ways to construct your HTML pages.

Interactively Creating a Query

To interactively create a query from which your HTML page will be built, follow these steps:

While this method works and is real easy to use for those who are novices with Transact-SQL, I found it to be an awkward and limiting interface for those who are competent in writing their own SQL queries. The Build a Query From a Database Hierarchy window is shown in Figure 23.2.

FIG. 23.2

Using the Web Assistant's Build A Query From a Database Hierarchy option.

Entering a Query as Freeform Text Option

With this option, there are no training wheels, you have to create your query as you see fit and make sure that it will act as you desire. Of course, with this option you can create a rowset that can be as complicated as your needs require, without restraints. In the example I am going to use here, I developed and tested the queries in ISQL/w, a utility program that comes with SQL-Server (see fig. 23.3).

FIG. 23.3

Using ISQL/w to create queries.

Looking at the Query window in Figure 23.3, you can see that I have two Select type queries in the window. The Web Assistant treats these as two table sets in the resulting HTML page. All I need to do is cut and paste the perfected query into the Web Assistant's Freeform Query text window, as shown in Figure 23.4. Listing 23.2 shows the query that I developed for this chapter.

Listing 23.2 Muti Select Query that Results in Two Rowsets Being Returned

SELECT * FROM Publishers
SELECT publishers.pub_name,titles.title,titles.type,
titles.price, authors.au_fname, authors.au_lname
FROM publishers Left Join titles ON publishers.pub_id = titles.Pub_id
LEFT JOIN titleauthor
ON titles.title_id = titleauthor.title_id
LEFT JOIN authors
ON titleauthor.au_id = authors.au_id
ORDER BY publishers.pub_id

FIG. 23.4

The perfected Transact-Sql Query pasted from ISQL/w into the Web Assistant.

Scheduling a Web Page Task

With the query now pasted into the Web Assistant, I move on to the next window, which includes a single drop down box titled Scheduling Options. There are five choices that determine the life span of your Web page task they are described in the following list:

This is a very flexible system for scheduling Web page updates. You can schedule one time tasks, triggered tasks, or regular timed schedules. Figure 23.5 shows the Web Assistant indow in the When Data changes Mode. The format of this window changes depending on your selection of a scheduling option.

FIG. 23.5

Setting a scheduling option in the Web Assistant.

Using File Options with the Web Assistant

The next Web Assistant page, after scheduling, is the Files Options page. Of course the first option is the final location path of the HTML page to be generated. You need to provide a fully qualified path, or a UNC path for the page to which you are writing. In the next option, you decide if you want to use a template file. I will go into more detail on this option in the "Building a VBScript Based Web Page" later in this chapter. For now I'll leave it unchecked and the Web Assistant will progress on to some basic page customizations. On the File Options page, there is also an option for including one URL link, or several links from a table in the database. Using this option, places one or more URL links at the bottom of the resulting Web page. You can view the File Options page in Figure 23.6.

FIG. 23.6

Filling in the File Options of the Web Assistant.

Formatting the Page with the Web Assistant

Don't get exited; there are not a lot of options here. You can select a header type for the page title from a drop down list that includes H1 to H6. You can also select fixed or proportional font, bold and/or italic text for the output data. You can also add a Date-time stamp to the top of the page and allow column names for the data tables. Figure 23.7 shows the resulting HTML page from the supplied query.

FIG. 23.7

The HTML page made by the Web Assistant shown in Explorer 3.0b1.

Building a VBScript-Based Web Page

If the Web page shown in Figure 23.7 knocked your socks off and met with your ideas of what a Web page should look like, move on to the next chapter because you are done here.

Still reading? Okay, let's build a real Web page using the Web Assistant. There are only two things you have to do: write a template file, and specify that file in the File Options page. In this example, I want to have a ComboBox loaded with publisher names. When I select a publisher from the ComboBox, I want a book printed by that publisher to appear in the text boxes below. The first thing we have to do is create the query that drives the page. That query is in Listing 23.2 shown earlier in the chapter. The next step is to create a template that the Web Assistant will use to make this page. The first part of the template's code populates the template with the ActixeX controls that will contain the data and lay out the visible part of the HTML page. This part is contained in Listing 24.3.

Listing 23.1ùQUEWEB2.TPL The Layout Portion of the Template File Including the ActiveX Control Declarations

<HTML>
<HEAD>
<TITLE>SQL Server Web Assistant Demo 2</TITLE>
</HEAD>
<BODY>
<OBJECT ID="ieList" WIDTH=1 HEIGHT=1
CLASSID="CLSID:8BD21D20-EC42-11CE-9E0D-00AA006002F3">
<PARAM NAME="ScrollBars" VALUE="3">
<PARAM NAME="DisplayStyle" VALUE="2">
<PARAM NAME="Size" VALUE="5151;811">
<PARAM NAME="MatchEntry" VALUE="0">
<PARAM NAME="FontCharSet" VALUE="0">
<PARAM NAME="FontPitchAndFamily" VALUE="2">
<PARAM NAME="FontWeight" VALUE="0">
</OBJECT>
<OBJECT ID="ieCombo" WIDTH=391 HEIGHT=24
CLASSID="CLSID:8BD21D30-EC42-11CE-9E0D-00AA006002F3">
<PARAM NAME="VariousPropertyBits" VALUE="746604571">
<PARAM NAME="DisplayStyle" VALUE="3">
<PARAM NAME="Size" VALUE="10336;635">
<PARAM NAME="MatchEntry" VALUE="1">
<PARAM NAME="ShowDropButtonWhen" VALUE="2">
<PARAM NAME="FontHeight" VALUE="200">
<PARAM NAME="FontCharSet" VALUE="0">
<PARAM NAME="FontPitchAndFamily" VALUE="2">
<PARAM NAME="FontWeight" VALUE="0">
</OBJECT>
<br>
<br>
TITLE:
<BR>
<INPUT TYPE=TEXT NAME="txtTitle" Size=100>
<BR>
TYPE:
<BR>
<INPUT TYPE=TEXT NAME="txtType" Size=100>
<BR>
PRICE:
<BR>
<INPUT TYPE=TEXT NAME="txtPrice" Size = 100>
<BR>
AUTHOR:
<BR>
<INPUT TYPE=TEXT NAME="txtName" Size = 100>

The ComboBox and ListBox need to be populated with the rowsets returned from the query. I should note here that a ListBox is being used to receive data in place of a data array. At the time of writing this book, arrays were not yet working in VBScript. By the time you read this, they should be working, which will make some of the contortions being performed here unnecessary. Anyway, what I do is load all of the query results into these two controls separating the fields with a semicolon. Use the <%insert_data_here%> place holder to mark where the data fields should be placed. The fields will be filled in the order that the query requests them in the Select part of the query. Take a look at that code now in Listing 23.4.

Listing 23.4ùQUEWEB2.TPL The Code that Inserts the Query's Data into the ActiveX Controls

<SCRIPT LANGUAGE="VBSCRIPT">
Dim Pubname
Dim AllData
<%begindetail%>
PubName = "<%insert_data_here%>" & ";" & "<%insert_data_here%>"
ieCombo.additem PubName
<%enddetail%>
<%begindetail%>
ielist.additem "<%insert_data_here%>" & ";" & "<%insert_data_here%>" _
& ";" & "<%insert_data_here%>" & ";" & "<%insert_data_here%>" _
& ";" & "<%insert_data_here%>" & ";" & "<%insert_data_here%>"
<%enddetail%>

The <%begindetail%><%enddetail%> pair mark off the boundaries of a given rowset. That means that the procedure will iterate through all of the rows returned and place the data in that rowset repeatedly, along with what ever other code is between those markers, in the resulting file. To understand what I mean, look at the corresponding section in the resulting HTML page in Listing 23.5.

Listing 23.5ùQUEWEB2.HTML The Resulting Section of Code Returned by the sp_makeweb Procedure

<SCRIPT LANGUAGE="VBSCRIPT">
ieCombo.additem "0736" & ";" & "New Moon Books"
ieCombo.additem "0877" & ";" & "Binnet & Hardley"
ieCombo.additem "1389" & ";" & "Algodata Infosystems"
ieCombo.additem "1622" & ";" & "Five Lakes Publishing"
ieCombo.additem "1756" & ";" & "Ramona Publishers"
ieCombo.additem "9901" & ";" & "GGG&G"
ieCombo.additem "9952" & ";" & "Scootney Books"
ieCombo.additem "9999" & ";" & "Lucerne Publishing"
...
...

What you see in Listing 24.5 is the results of the first <%begindetail%><%enddetail%> pair. Finally to complete our template file, add the VBScript code that parses the data into its component parts and loads those parts into the TextBoxes. There is also a click event of the ieCombo that calls the parsing procedure. It's all in Listing 24.6.

Listing 23.6ùQUEWEB2.TPL The click Event and Parsing Procedures

Sub ieCombo_Onclick
SeparateData
End sub
Sub SeparateData
Dim lngStart
Dim lngEnd
Dim strItems
Dim strName
strItems = ielist.list(ieCombo.ListIndex)
lngStart = 1
lngEnd = instr(lngStart,strItems,";")
txtTitle.Value = mid(strItems,lngStart,lngEnd-lngStart)
lngStart=lngEnd + 1
lngEnd = instr(lngStart,strItems,";")
txtType.Value = mid(strItems,lngStart,lngEnd-lngStart)
lngStart=lngEnd + 1
lngEnd = instr(lngStart,strItems,";")
txtPrice.Value = mid(strItems,lngStart,lngEnd-lngStart)
lngStart=lngEnd + 1
lngEnd = instr(lngStart,strItems,";")
strName = mid(strItems,lngStart,lngEnd-lngStart)
lngStart=lngEnd + 1
lngEnd = instr(lngStart,strItems,";")
strName = strName & " " & mid(strItems,lngStart,lngEnd-lngStart)
lngStart=lngEnd + 1
txtName = strName
End Sub
</SCRIPT>
</BODY>
</HTML>

After this template file is stored include the template file in the Web Assistant's Option page by specifying the fully qualified path to it. After completing the remaining steps of the Web Assistant's process, the page will be created by SQL Server. That page will look like the one in Figure 23.8.

FIG. 23.8

The queweb2.html page displayed in Explorer 3.0b1.

The above example shows how the Web Assistant can help you to create your Web pages. As another example, using only the sp_makewebtask procedure, I'll show how you can construct procedures that will do the same thing without the Web Assistant. First I'll make a slightly prettier template file that uses a table to contain the detail TextBoxes. The code in Listing 23.7 shows that template file.

Listing 23.7ùSQL6WEB.TPL A Template File Using a Table and Text Box Combination to Display Data

<HTML>
<HEAD>
<TITLE>SQL Server Multiple Queries with Template Web Sample</TITLE>
<BODY>
<H1>Books For Sale</H1>
<HR>
<OBJECT ID="ieList" WIDTH=1 HEIGHT=1
CLASSID="CLSID:8BD21D20-EC42-11CE-9E0D-00AA006002F3">
<PARAM NAME="ScrollBars" VALUE="3">
<PARAM NAME="DisplayStyle" VALUE="2">
<PARAM NAME="Size" VALUE="5151;811">
<PARAM NAME="MatchEntry" VALUE="0">
<PARAM NAME="FontCharSet" VALUE="0">
<PARAM NAME="FontPitchAndFamily" VALUE="2">
<PARAM NAME="FontWeight" VALUE="0">
</OBJECT>
<OBJECT ID="ieCombo" WIDTH=391 HEIGHT=24
CLASSID="CLSID:8BD21D30-EC42-11CE-9E0D-00AA006002F3">
<PARAM NAME="VariousPropertyBits" VALUE="746604571">
<PARAM NAME="DisplayStyle" VALUE="3">
<PARAM NAME="Size" VALUE="10336;635">
<PARAM NAME="MatchEntry" VALUE="1">
<PARAM NAME="ShowDropButtonWhen" VALUE="2">
<PARAM NAME="FontHeight" VALUE="200">
<PARAM NAME="FontCharSet" VALUE="0">
<PARAM NAME="FontPitchAndFamily" VALUE="2">
<PARAM NAME="FontWeight" VALUE="0">
</OBJECT>
<br>
<br>
<TABLE BORDER=5>
<TR BGCOLOR="YELLOW">
<TH Size = 35>Table Name</TH>
<TH Size = 35>Table Data</TH>
</TR>
<TR>
<TD BGCOLOR="TAN">AUTHOR ORDER</TD>
<TD>
<Input Type=Text Size=30 ID="AUTHORD" VALUE="TEST">
</TD></TR>
<TR>
<TD BGCOLOR="TAN">AUTHOR NAME</TD>
<TD>
<Input Type=Text Size=30 ID="AuthName" VALUE="TEST">
</TD></TR>
<TR>
<TD BGCOLOR="TAN">PRICE OF BOOK</TD>
<TD>
<Input Type=Text Size=30 ID="AUTHPrice" VALUE="TEST">
</TD></TR>
<TR>
<TD BGCOLOR="TAN">GROSS SALES</TD>
<TD>
<Input Type=Text Size=30 ID="AUTHSales" VALUE="TEST">
</TD></TR>
<TR>
<TD BGCOLOR="TAN">PUBLISHER ID</TD>
<TD>
<Input Type=Text Size=30 ID="AUTHPubID" VALUE="TEST">
</TD></TR>
</TABLE>
<P>
<Script Language="VBScript">
blnLoading = True
<%begindetail%>
ieCombo.additem "<%insert_data_here%>"
ieList.addItem "<%insert_data_here%>" _
& ";" & "<%insert_data_here%>" _
& " " & "<%insert_data_here%>" _
& ";" & "<%insert_data_here%>" _
& ";" & "<%insert_data_here%>" _
& ";" & "<%insert_data_here%>"
<%enddetail%>
blnLoading = False
Sub ieCombo_Click()
dim lngItem
If blnLoading then Exit Sub
SeparateData
End Sub
Sub SeparateData
Dim lngStart
Dim lngEnd
Dim strItems
strItems = ielist.list(ieCombo.ListIndex)
lngStart = 1
lngEnd = instr(lngStart,strItems,";")
AuthOrd.Value=mid(strItems,lngStart,lngEnd-lngStart)
lngStart=lngEnd + 1
lngEnd = instr(lngStart,strItems,";")
AuthName.Value=mid(strItems,lngStart,lngEnd-lngStart)
lngStart=lngEnd + 1
lngEnd = instr(lngStart,strItems,";")
AuthPrice.Value=mid(strItems,lngStart,lngEnd-lngStart)
lngStart=lngEnd + 1
lngEnd = instr(lngStart,strItems,";")
AuthSales.Value=mid(strItems,lngStart,lngEnd-lngStart)
lngStart=lngEnd + 1
End Sub
</Script>
<P>
<A HREF = "http://www.w3.org.pub/WWW/">The World Wide Web Consortium</A><P>
<A HREF = "http://www.w3.org/hypertext/WWW/MarkUp/html3/contents.html">HTML 3 specs</A><P>
<A HREF = "http://www.microsoft.com">MICROSOFT</A><P>
</BODY>
</HTML>

To use this template in conjunction with the sp_makewebtask stored procedure we'll call up the ISQL/w file. And enter the text shown in Listing 23.10. The ISQL/w query screen looks like Figure 24.9.

FIG. 23.9

Using the ISQL/w program to execute the sp_makewebtask procedure.

Listing 24.2ùWEBTEST4.SQL A Typical sp_makewebtask Procedure

USE pubs
go
EXECUTE sp_makewebtask @outputfile = 'D:\QUE\CHAPT24\SQL6WEB.HTM',
@query = 'SELECT title, au_ord, au_fname, au_lname, price, ytd_sales, pub_id
FROM authors, titles, titleauthor
WHERE authors.au_id = titleauthor.au_id
AND titles.title_id = titleauthor.title_id',
@templatefile = 'D:\QUE\CHAPT24\SQL6WEB.TPL',
@dbname = 'PUBS', @rowcnt = 25, @whentype = 1
go

Just by executing this query the web page shown in Figure 23.10 will be created.

FIG. 23.10

The Web page created by the query in Listing 23.8.

From Here...

The Web extensions for SQL Server provide a very attractive alternative to the more complex IDC method of providing data. The stress on the server is also minimized since the pages are not created during user demand time. The Web server merely sends the already existing pages out. The examples shown here are really simple and do not touch on too deeply on more complex strategies that the Web Extensions could be targeted towards. What was presented here was really intended to show you how you could use VBScript to enhance the Web pagers produced through the use of customized template files.


| Previous Chapter | Next Chapter |

| Search | Table of Contents | Book Home Page | Buy This Book |

| Que Home Page | Digital Bookshelf | Disclaimer |


To order books from QUE, call us at 800-716-0044 or 317-361-5400.

For comments or technical support for our books and software, select Talk to Us.

© 1996, QUE Corporation, an imprint of Macmillan Publishing USA, a Simon and Schuster Company.